package com.wwtech.epp.ui.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseFragment;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.entity.WorkState;
import com.wwtech.epp.ui.activity.main.component.DaggerMyComponent;
import com.wwtech.epp.ui.activity.main.contract.MyContract;
import com.wwtech.epp.ui.activity.main.module.MyModule;
import com.wwtech.epp.ui.activity.main.presenter.MyPresenter;
import com.wwtech.epp.ui.activity.mine.MyInfoActivity;
import com.wwtech.epp.ui.activity.mine.SettingActivity;
import com.wwtech.epp.ui.activity.work.MyDailyActivity;
import com.wwtech.epp.ui.activity.work.WebViewActivity;
import com.wwtech.epp.ui.adapter.work.WorkStateAdapter;
import com.wwtech.epp.utils.ToastUtil;
import com.wwtech.epp.utils.UIUtils;
import com.wwtech.epp.utils.WorkUtil;
import com.wwtech.epp.view.GlideCircleTransform;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.main
 * @Description: $description
 * @date 2017/08/21 11:14:48
 */

public class MyFragment extends BaseFragment implements MyContract.View {

    @Inject
    MyPresenter mPresenter;

    @Inject
    WorkStateAdapter workStateAdapter;
    @BindView(R.id.seal_more)
    ImageView sealMore;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.layout_top)
    LinearLayout layoutTop;
    @BindView(R.id.iv_avater)
    ImageView ivAvater;
    @BindView(R.id.job_title)
    TextView jobTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.daily)
    TextView daily;
    @BindView(R.id.setting)
    TextView setting;
    @BindView(R.id.help)
    TextView help;
    @BindView(R.id.rl_mine)
    RelativeLayout rlMine;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.work_state)
    TextView workState;
    @BindView(R.id.recycerView_work_state)
    RecyclerView recycerViewWorkState;
    String tempStatus = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, null);
        ButterKnife.bind(this, view);
        Bundle mBundle = getArguments();
        EventBus.getDefault().register(this);
        layoutTop.setPadding(0, (int) (UIUtils.getStatusBarHeight(getActivity()) * 0.8), 0, 0);
        tvTitle.setText("我的");
        tvName.setText(ContainValue.userLogin.getData().getNickName());
        jobTitle.setText(ContainValue.userLogin.getData().getOrgTitle());    //ContainValue.userLogin.getData().getDepartment() + "  " +
        Glide.with(this)
                .load(ContainValue.userLogin.getData().getAvatar())
//                .skipMemoryCache(true)
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transform(new GlideCircleTransform(getActivity()))
                .into(ivAvater);
        recycerViewWorkState.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycerViewWorkState.setAdapter(workStateAdapter);
        ArrayList<WorkState> workStatesList = new ArrayList<>();
        workStatesList.add(new WorkState("工作中", "工作中".equals(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()))? true : false));
        workStatesList.add(new WorkState("生病中", "生病中".equals(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()))? true : false));
        workStatesList.add(new WorkState("请假中", "请假中".equals(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()))? true : false));
        workStatesList.add(new WorkState("出差中", "出差中".equals(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()))? true : false));
        workStatesList.add(new WorkState("会议中", "会议中".equals(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()))? true : false));
        workStatesList.add(new WorkState("休假中", "休假中".equals(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()))? true : false));
        workState.setText(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()));
        workStateAdapter.setNewData(workStatesList);
        workStateAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (workStateAdapter.getData().get(i).isCheck()) {
                    return;
//                    workStateAdapter.getData().get(i).setCheck(false);
//                    tempStatus = "";
                } else {
                    for (int j = 0; j < workStateAdapter.getData().size(); j++) {
                        workStateAdapter.getData().get(j).setCheck(false);
                    }
                    workStateAdapter.getData().get(i).setCheck(true);
                    tempStatus = workStateAdapter.getData().get(i).getState();
                }
                workStateAdapter.notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setImg(String content) {
        if (content.equals("alertAvater")) {
            Glide.with(this)
                    .load(ContainValue.userLogin.getData().getAvatar())
//                .skipMemoryCache(true)
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .transform(new GlideCircleTransform(getActivity()))
                    .into(ivAvater);
        }
    }

    @Override
    protected void setupFragmentComponent() {
        DaggerMyComponent
                .builder()
                .appComponent(((AppConfig) getActivity().getApplication()).getApplicationComponent())
                .myModule(new MyModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(MyContract.MyContractPresenter presenter) {
        mPresenter = (MyPresenter) presenter;
    }

    @Override
    protected void initDataFromLocal() {

    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void updateWorkStateSuceess() {
        ToastUtil.displayShortToast("修改状态成功");
        workState.setText(tempStatus);
        ContainValue.userLogin.getData().setWorkStatus(WorkUtil.workStatusParse(tempStatus));
        tempStatus = "";
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.rl_mine, R.id.daily, R.id.setting, R.id.help, R.id.work_state})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_mine:
                startActivity(MyInfoActivity.class);
                break;
            case R.id.daily:
                startActivity(new Intent(getActivity(), MyDailyActivity.class));
                break;
            case R.id.setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.help:
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url", "http://www.vv-tech.com/static/help.html");
                intent.putExtra("title", "使用手册");
                startActivity(intent);
                break;
            case R.id.work_state:
                if (recycerViewWorkState.getVisibility() == View.VISIBLE) {
                    recycerViewWorkState.setVisibility(View.GONE);
                    preUpdateWorkState();
                } else {
                    tempStatus = "";
                    recycerViewWorkState.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
        }
    }

    private void preUpdateWorkState() {
        if (tempStatus.equals("")) {
            return;
        }
        if (tempStatus.equals(WorkUtil.workStatusParse(ContainValue.userLogin.getData().getWorkStatus()))) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("uid", ContainValue.userLogin.getData().getUserId() + "");
        map.put("status", WorkUtil.workStatusParse(tempStatus) + "");
        mPresenter.updateWorkState(ContainValue.userLogin.getData().getUserId() + "", map);
     }

}